package customers.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import customers.dal.daointerface.MarketCouponDAO;
import customers.dal.dataobject.MarketCouponDO;
import customers.model.MarketCoupon;
import customers.model.MarketCouponReq;
import customers.model.MarketCouponResp;
import customers.service.MarketCouponService;

public class MarketCouponServiceImpl implements MarketCouponService {
	@Autowired
	private MarketCouponDAO marketCouponDAO;

	@Override
	public MarketCouponResp<MarketCoupon> insertMarketCoupon(MarketCoupon marketCoupon) {
        MarketCouponDO marketCouponDO = new MarketCouponDO();
        
        marketCouponDO.setAmount(marketCoupon.getAmount());
        marketCouponDO.setBusinessId(marketCoupon.getBusinessId());
        marketCouponDO.setCategory(marketCoupon.getCategory());
        marketCouponDO.setCouponStatus(marketCoupon.getCouponStatus());
        marketCouponDO.setCustomerId(marketCoupon.getCustomerId());
        
		marketCouponDO.setEndTime(marketCoupon.getEndTime());
		marketCouponDO.setStartTime(marketCoupon.getStartTime());
		marketCouponDO.setFreezeTime(marketCoupon.getFreezeTime());
		marketCouponDO.setLimitAmountMin(marketCoupon.getLimitAmountMin());
		marketCouponDO.setLimitDurationMax(marketCoupon.getLimitDurationMax());
		marketCouponDO.setLimitDurationMin(marketCoupon.getLimitDurationMin());
		
		marketCouponDO.setMobile(marketCoupon.getMobile());
		marketCouponDO.setRuleDesc(marketCoupon.getRuleDesc());
		marketCouponDO.setRuleName(marketCoupon.getRuleName());
		marketCouponDO.setSendRuleType(marketCoupon.getSendRuleType());
		
		marketCouponDO.setSource(marketCoupon.getSource());
		marketCouponDO.setSourceDesc(marketCoupon.getSourceDesc());
		marketCouponDO.setVerificationTime(marketCoupon.getVerificationTime());
		marketCouponDO.setSendType(marketCoupon.getSendType());
        
		int a = marketCouponDAO.insert(marketCouponDO);
		MarketCouponResp<MarketCoupon> resp = new MarketCouponResp<>();
		if(a==1){
			resp.setResultCode("0000");
			resp.setResultMsg("添加成功");
			resp.setBody(marketCoupon);
		}else{
			resp.setResultCode("9999");
			resp.setResultMsg("添加失败");
		}
		return resp;
	}
    
	/**
	 * 按会员ID查询优惠券信息
	 */
	@Override
	public MarketCouponResp<List<MarketCoupon>> selectCouponByCustomerId(MarketCouponReq<MarketCoupon> req) {
		int customerId = req.getBody().getCustomerId();
		List<MarketCoupon> list = po2Vo(marketCouponDAO.selectCouponByCustomerId(customerId));
		MarketCouponResp<List<MarketCoupon>> resp = new MarketCouponResp<>();
		resp.setBody(list);
		resp.setResultCode("0000");
		resp.setResultMsg("查询成功");
		return resp;
	}
	
	private List<MarketCoupon> po2Vo(List<MarketCouponDO> listDO){
		List<MarketCoupon> list = new ArrayList<>();
		MarketCoupon marketCoupon = new MarketCoupon();
		for(MarketCouponDO marketCouponDO:listDO){
			marketCoupon.setAmount(marketCouponDO.getAmount());
	        marketCoupon.setBusinessId(marketCouponDO.getBusinessId());
	        marketCoupon.setCategory(marketCouponDO.getCategory());
	        marketCoupon.setCouponStatus(marketCouponDO.getCouponStatus());
	        marketCoupon.setCustomerId(marketCouponDO.getCustomerId());
	        
			marketCoupon.setEndTime(marketCouponDO.getEndTime());
			marketCoupon.setStartTime(marketCouponDO.getStartTime());
			marketCoupon.setFreezeTime(marketCouponDO.getFreezeTime());
			marketCoupon.setLimitAmountMin(marketCouponDO.getLimitAmountMin());
			marketCoupon.setLimitDurationMax(marketCouponDO.getLimitDurationMax());
			marketCoupon.setLimitDurationMin(marketCouponDO.getLimitDurationMin());
			
			marketCoupon.setMobile(marketCouponDO.getMobile());
			marketCoupon.setRuleDesc(marketCouponDO.getRuleDesc());
			marketCoupon.setRuleName(marketCouponDO.getRuleName());
			marketCoupon.setSendRuleType(marketCouponDO.getSendRuleType());
			
			marketCoupon.setSource(marketCouponDO.getSource());
			marketCoupon.setSourceDesc(marketCouponDO.getSourceDesc());
			marketCoupon.setVerificationTime(marketCouponDO.getVerificationTime());
			marketCoupon.setSendType(marketCouponDO.getSendType());
			marketCoupon.setCreateTime(marketCouponDO.getCreateTime());
			
			marketCoupon.setModifyTime(marketCouponDO.getModifyTime());
			marketCoupon.setCouponId(marketCouponDO.getCouponId());
			list.add(marketCoupon);
		}
		return list;
	}
    
	/**
	 * 冻结处理
	 */
	@Override
	public MarketCouponResp<MarketCoupon> frezzeCoupon(MarketCouponReq<MarketCoupon> marketCouponReq) {
		String businessId = marketCouponReq.getBody().getBusinessId();
	    Date date = new Date();
	    Map map = new HashMap();
	    map.put("businessId", businessId);
	    map.put("couponStatus", 2);
	    map.put("freezeTime", date);
	    int a = marketCouponDAO.updateCouponStatus(map);
	    MarketCouponResp<MarketCoupon> resp = new MarketCouponResp<>();
	    if(a==1){
	    	resp.setResultCode("0000");
	    	resp.setResultMsg("处理成功");
	    }else{
	    	resp.setResultCode("9999");
	    	resp.setResultMsg("处理失败");
	    }
		return resp;
	}
	
	
    
	/**
	 * 核销
	 */
	@Override
	public MarketCouponResp<MarketCoupon> verificationCoupon(
			MarketCouponReq<MarketCoupon> marketCouponReq) {
		
		String businessId = marketCouponReq.getBody().getBusinessId();
		MarketCouponResp<MarketCoupon> resp = new MarketCouponResp<>();
		MarketCouponDO marketCouponDO = marketCouponDAO.selectCouponByBusinessId(businessId);
		if(marketCouponDO.getCouponStatus()==3){
			resp.setResultCode("6666");
		    resp.setResultMsg("该优惠券已被使用");
		}else{
		    Date date = new Date();
		    Map map = new HashMap();
		    map.put("businessId", businessId);
		    map.put("couponStatus", 3);
		    map.put("verificationTime", date);
		    int a = marketCouponDAO.updateCouponStatusForVerification(map);
		    if(a==1){
		    	resp.setResultCode("0000");
		    	resp.setResultMsg("处理成功");
		    }else{
		    	resp.setResultCode("9999");
		    	resp.setResultMsg("处理失败");
		    }
			
		}
		return resp;
	}
	
    /**
     * 解冻
     */
	@Override
	public MarketCouponResp<MarketCoupon> unfrezzeCoupon(MarketCouponReq<MarketCoupon> marketCouponReq) {
		String businessId = marketCouponReq.getBody().getBusinessId();
		MarketCouponResp<MarketCoupon> resp = new MarketCouponResp<>();
		MarketCouponDO marketCouponDO = marketCouponDAO.selectCouponByBusinessId(businessId);
		if(marketCouponDO.getCouponStatus()==1){
			resp.setResultCode("6666");
		    resp.setResultMsg("该优惠券未被冻结");
		}else{
			Map map = new HashMap();
		    map.put("businessId", businessId);
		    map.put("couponStatus", 1);
		    map.put("freezeTime", null);
		    int a = marketCouponDAO.updateCouponStatus(map);
		    
		    if(a==1){
		    	resp.setResultCode("0000");
		    	resp.setResultMsg("处理成功");
		    }else{
		    	resp.setResultCode("9999");
		    	resp.setResultMsg("处理失败");
		    }
			
		}
	  
		return resp;
	}
	

}
